package com.example.drink_order_system.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.drink_order_system.R;

public class WelcomeActivity extends AppCompatActivity {

    private CountDownTimer countDownTimer;
    private long timeLeftInMillis=4000;
    private TextView tvCountdown;
    private static int lag = 0;
    @Override
    protected void onCreate(Bundle saveInstanceState) {

        super.onCreate(saveInstanceState);

        setContentView(R.layout.activity_welcome);
        //初始化控件
        tvCountdown = findViewById(R.id.tv_countdown);

        //启动倒计时

      tvCountdown.setOnClickListener(new View.OnClickListener() {
            @Override
            public  void onClick(View v){
                lag = 1;
                startActivity(new Intent(WelcomeActivity.this,MainActivity.class));
            }
        });
        startCountDown();
    }

    private void startCountDown(){
            countDownTimer=new CountDownTimer(timeLeftInMillis,1000){
            @Override
            public void onTick(long millisUntilFinished){
                timeLeftInMillis=millisUntilFinished;
                int secondsRemaining=(int)(millisUntilFinished/1000);
                tvCountdown.setText("跳过  "+secondsRemaining+"s");
            }
            @Override
            public void onFinish(){
                if (lag==0){
                    startActivity(new Intent(WelcomeActivity.this,MainActivity.class));
                }
                finish();
            }
        }.start();
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        if(countDownTimer!=null){
            countDownTimer.cancel();
        }
    }
}
